<template>
	<view>
		<view class=" u-p-r-30 u-p-l-30 u-m-t-20">
			<u-row>
				<u-col span="11" text-align="center">
						<u-search placeholder="输入商品编号/名称/货号搜索" :clearabled="true" v-model="keyword" border-color="#606266" @custom="search" @search="search"></u-search>
				</u-col>
				<u-col span="1" text-align="center" @click="scang()">
					<view class="u-m-t-14 weikaishi2">
						<u-icon name="scan" color="#2b85e4" size="43"></u-icon>
					</view>
				</u-col>
			</u-row>
		</view>
		<view class=" u-p-r-30 u-p-l-30 u-m-t-20" >
			<u-row>
				<u-col span="11" text-align="center">
						<u-search placeholder="输入库位编号" :clearabled="true" v-model="keyword2" border-color="#606266" @custom="search2" @search="search2"></u-search>
				</u-col>
				<u-col span="1" text-align="center" @click="can()">
					<view class="u-m-t-14 weikaishi2">
						<u-icon name="scan" color="#2b85e4" size="43"></u-icon>
					</view>
				</u-col>
			</u-row>
		</view>
		<view class=" u-p-r-30 u-p-l-30 u-m-t-20" >
				<u-input type="select" @click="getp()" placeholder="选择位置"/>
		</view>
		
		<u-card padding="16" v-if="findtype == 'p'">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="10" text-align="left">
							<view class=" u-m-t-2 weikaishi2">
								<span v-html="dataList.warehouseName+dataList.areaName+dataList.positionNumber"></span>
							</view>
						</u-col>
						<u-col span="2" text-align="center">
							<view class="u-line-1">
								<span><u-button type="default" plain="true" size="mini" @click="add(dataList)">新增</u-button></span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<view class="wrap">
					<u-row>
						<u-col span="6" text-align="center">
							<view class="u-line-1 u-m-t-4 weikaishi3">
								<span>数量</span>
							</view>
						</u-col>
						<u-col span="6" text-align="center">
							<view class="u-line-1 u-m-t-4 weikaishi3">
								<span>品类</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="6" text-align="center">
							<view class="u-line-1 weikaishi">
								<span v-html="dataList.totalNum+'件'"></span>
							</view>
						</u-col>
						<u-col span="6" text-align="center">
							<view class="u-line-1 weikaishi">
								<span v-html="dataList.totalgoodsType+'个'"></span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
		</u-card>
		
		<u-card padding="16" v-if="findtype == 'p'" v-for="(item,index) in dataList.list" :key="index">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="12" text-align="left">
							<view>
								<span>名称：{{item.goodsName}}</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="6" text-align="left">
							<view class="u-line-1 u-m-t-10">
								<span>编号：{{item.goodsNumber}}</span>
							</view>
						</u-col>
						<u-col span="6" text-align="center">
							<view class="u-line-1 u-m-t-10">
								<span>货号：{{item.goodsNo}}</span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="12" text-align="left">
							<view class="u-line-1 u-m-t-10">
								<span>{{item.num}}件*{{item.trayNum}}托</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<view class="wrap">
					<u-row>
						<u-col span="5" text-align="center">
							<view class="weikaishi u-line-1 u-m-t-10">
								<span>批次</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class="weikaishi u-line-1 u-m-t-10">
								<span>托盘</span>
							</view>
						</u-col>
						<u-col span="4" text-align="center">
							<view class="weikaishi u-line-1 u-m-t-10">
								<span>数量(件)</span>
							</view>
						</u-col>
					</u-row>
					<u-row v-for="(item1,index1) in item.list2" :key="index1">
						<u-col span="5" text-align="center">
							<view class="u-line-1 u-m-t-25">
								<!-- <span><u-input v-model="item.list2[index1].producetime"  type="select"  border-color="#1999dc" border="true" @click="getT(item1)"/></span> -->
								<span>{{item1.goodsbatchs}}</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class="u-line-1 u-m-t-25">
								<span>{{item1.trayNumber}}</span>
							</view>
						</u-col>
						<u-col span="4" text-align="center">
							<view class="u-line-1 u-m-t-10">
								<span><u-input v-model="item.list2[index1].num" type="digit" border-color="#1999dc" border="true" placeholder="输入更正数.."/></span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="foot-butten" slot="foot">
				<view :class="flex">
					<u-col span="12" text-align="center" @click="quedingp(item)">
						<view class="u-line-1 u-m-12 lanse">
							确定
						</view>
					</u-col>
				</view>
			</view>
		</u-card>
		
		<u-card padding="16" v-if="findtype == 'g'">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="12" text-align="left">
							<view class=" u-m-t-2 weikaishi2">
								<span v-html="dataList2.goodsName+'，'+dataList2.goodsNo"></span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<view class="wrap">
					<u-row>
						<u-col span="6" text-align="center">
							<view class="u-line-1 u-m-t-4 weikaishi3 lanse">
								<span v-html="'总数量：'+dataList2.sumNum+'件'"></span>
							</view>
						</u-col>
						<u-col span="6" text-align="center">
							<view class="u-line-1 u-m-t-4 weikaishi3 lanse">
								<span v-html="'总体积：'+dataList2.sumVolume+'m³'"></span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
		</u-card>
		
		<u-card padding="16" v-if="findtype == 'g'" v-for="item in dataList2.list" :key="item.positionId">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="10" text-align="left">
							<view class="u-line-1 u-m-t-10">
								<span>{{item.warehouseName+item.areaName+item.positionNumber}}</span>
							</view>
						</u-col>
						<u-col span="2" text-align="center">
							<view class="u-line-1"> 
								<span><u-button type="default" plain="true" size="mini" @click="add2(item)">新增</u-button></span>
							</view>
						</u-col>
					</u-row>
					<u-row>
						<u-col span="12" text-align="left">
							<view class="u-line-1 u-m-t-10">
								<span>库存数量：{{item.sumNum}}件*{{item.trayNum}}托</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<view class="wrap">
					<u-row>
						<u-col span="4" text-align="center">
							<view class="weikaishi u-line-1 u-m-t-10">
								<span>批次</span>
							</view>
						</u-col>
						<u-col span="4" text-align="center">
							<view class="weikaishi u-line-1 u-m-t-10">
								<span>托盘</span>
							</view>
						</u-col>
						<u-col span="4" text-align="center">
							<view class="weikaishi u-line-1 u-m-t-10">
								<span>数量(件)</span>
							</view>
						</u-col>
					</u-row>
					<u-row v-for="(item1,index) in item.list2" :key="index">
						<u-col span="5" text-align="center">
							<view class="u-line-1 u-m-t-25">
								<!-- <span><u-input v-model="item.list2[index].producetime"  type="select"  border-color="#1999dc" border="true" @click="getTg(item1)"/></span> -->
								<span>{{item1.goodsbatchs}}</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class="u-line-1 u-m-t-25">
								<span>{{item1.trayNumber}}</span>
							</view>
						</u-col>
						<u-col span="4" text-align="center">
							<view class="u-line-1 u-m-t-10">
								<span><u-input v-model="item.list2[index].num" type="digit" border-color="#1999dc" border="true" placeholder="输入更正数.."/></span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="foot-butten" slot="foot">
				<view :class="flex">
					<u-col span="12" text-align="center" @click="quedingg(item)">
						<view class="u-line-1 u-m-12 lanse">
							确定
						</view>
					</u-col>
				</view>
			</view>
		</u-card>
		
		<u-popup v-model="popupShow2" mode="center" closeable width="550rpx" height="600rpx" border-radius="10">
			<view class=" u-p-r-30 u-p-l-30 u-m-t-20" style="margin-top: 40rpx;">
				<u-search placeholder="输入商品编号/名称/货号搜索" :clearabled="true" v-model="keyword" border-color="#606266" @custom="search" @search="search"></u-search>
			</view>
			<view class="wrap">
				<u-row v-for="item in glist" :key="item.id" @click="xuanzegoods(item)">
					<u-col span="12" text-align="center">
						<view class="u-m-t-10 weikaishi lanse">
							<span>{{item.goodsNumber}},{{item.goodsName}},{{item.goodsNo}}</span>
						</view>
					</u-col>
				</u-row>
			</view>
		</u-popup>
		
		<view>
			<u-select v-model="popupShow3" mode="mutil-column-auto" :list="plist" @confirm="confirm"></u-select>
		</view>
		
		<view>
			<u-calendar v-model="show3" mode="date" max-date="2070-01-01" @change="change"></u-calendar>
		</view>
		<view>
			<u-calendar v-model="show4" mode="date" max-date="2070-01-01" @change="change2"></u-calendar>
		</view>
		
		<u-popup v-model="popupShow4" mode="center" closeable width="600rpx" height="500rpx" border-radius="10">
			<view class="u-text-center u-m-t-20">增加库存</view>
			<view class=" u-m-20">
				<u-input v-model="form.goodsName" type="select" @click="getgoo()" placeholder="请选择商品"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="form.trueNum" type="digit" border="true"  placeholder="请输入数量"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="form.newgoodsbatchs" type="select" @click="getbatchs()" border="true" placeholder="请选择生产日期"/>
			</view>
			<view class=" u-m-20">
				<u-input v-model="form.goodsbatchs" border="true"  placeholder="请输入批次"/>
			</view>
			<view style="height: 80rpx;text-align: center;">
				<view class="close-btn u-m-t-40">
					<u-button size="medium" @click="define4()">确定</u-button>
				</view>
			</view>
		</u-popup>
		
		<view>
			<u-calendar v-model="show5" mode="date" max-date="2070-01-01" @change="change3"></u-calendar>
		</view>
		
		<u-popup v-model="popupShow5" mode="center" closeable width="550rpx" height="600rpx" border-radius="10">
			<view class=" u-p-r-30 u-p-l-30 u-m-t-20" style="margin-top: 40rpx;">
				<u-search placeholder="输入商品编号/名称/货号搜索" :clearabled="true" v-model="keyword4" border-color="#606266" @custom="search3" @search="search3"></u-search>
			</view>
			<view class="wrap">
				<u-row v-for="item in glist" :key="item.id" @click="xuanzegoods2(item)">
					<u-col span="12" text-align="center">
						<view class="u-m-t-10 weikaishi lanse">
							<span>{{item.goodsNumber}},{{item.goodsName}},{{item.goodsNo}}</span>
						</view>
					</u-col>
				</u-row>
			</view>
		</u-popup>
		
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form:{
					goodsId:'',
					goodsNumber:'',
					goodsName:'',
					warehouseId:'',
					warehouseName:'',
					areaId:'',
					areaName:'',
					positionId:'',
					positionNumber:'',
					trueNum:'',
					goodsbatchs:'',
					newgoodsbatchs:''
				},
				flex:'',
				findtype:'',
				keyword:'',
				keyword2:'',
				keyword3:'',
				popupShow2:false,
				glist:[],
				positionNumber:'',
				popupShow3:false,
				popupShow4:false,
				popupShow5:false,
				show3:false,
				show4:false,
				show5:false,
				plist:[],
				dataList:[],
				dataList2:[],
				pinfo:{},
				ginfo:{},
				
				confirmPositionId:'',
				searchPositionNumber:'',
				xuanzeGoodsId:'',
				sousuotype:'',
			};
		},
		onLoad() {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			
			this.$http.post('/rest/storage/getWarehouselist',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				this.plist = res.data.data
			})
		},
		methods:{
			scang(){
				let that = this
				// 只允许通过相机扫码
				uni.scanCode({
					success: function (res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.keyword = res.result
						that.search()
					}
				});
			},
			can(){
				let that = this
				// 只允许通过相机扫码
				uni.scanCode({
					success: function (res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.keyword2 = res.result
						that.search2()
					}
				});
			},
			add2(item){
				this.form.warehouseId = item.warehouseId
				this.form.warehouseName = item.warehouseName
				this.form.areaId = item.areaId
				this.form.areaName = item.areaName
				this.form.positionId = item.positionId
				this.form.positionNumber = item.positionNumber
				this.popupShow4 = true
			},
			define4(){
				if(typeof this.form.goodsId == 'undefined' || this.form.goodsId == null || this.form.goodsId == ""){
					uni.showToast({
						title: "请选择商品",
						icon: 'none'
					})
					return
				}
				if(typeof this.form.trueNum == 'undefined' || this.form.trueNum == null || this.form.trueNum == ""){
					uni.showToast({
						title: "请输入数量",
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '正在提交'
				})
				this.$http.post('/rest/moveDisk/addStorage',{
				},{
					params: this.form,
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					if(res.data.code == 200) {
						this.popupShow4 = false
						this.$refs.uToast.show({
							title: '添加成功',
							type: 'success'
						})
						this.form.goodsId = ''
						this.form.goodsNumber = ''
						this.form.goodsName = ''
						this.form.trueNum = ''
						this.form.goodsbatchs = ''
						this.form.newgoodsbatchs = ''
						setTimeout(() => {
							if(this.sousuotype == '1'){
								this.findByListGoodsType(this.searchPositionNumber)
							} else if(this.sousuotype == '2'){
								this.findByListGoodsType2(this.confirmPositionId)
							} else if(this.sousuotype == '3'){
								this.findByGoods(this.xuanzeGoodsId)
							}
						}, 500)
					} else {
						this.$refs.uToast.show({
							title: res.data.message,
							type: 'error'
						})
					}
				})
			},
			getgoo(){
				this.search3()
				this.popupShow5 = true
			},
			add(item){
				this.form.warehouseId = this.dataList.warehouseId
				this.form.warehouseName = this.dataList.warehouseName
				this.form.areaId = this.dataList.areaId
				this.form.areaName = this.dataList.areaName
				this.form.positionId = this.dataList.positionId
				this.form.positionNumber = this.dataList.positionNumber
				this.popupShow4 = true
			},
			xuanzegoods2(item){
				this.form.goodsId = item.id
				this.form.goodsNumber = item.goodsNumber
				this.form.goodsName = item.goodsName
				this.popupShow5 = false
			},
			search3(){
				this.$http.post('/rest/storage/goodslist',{
				},{
					params: {
						condition:this.keyword4
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					let arr = []
					let inta = 0
					for(let item1 of res.data.data) {
						if(inta != 10){
							arr.push(item1)
						} else {
							break
						}
						inta++
					}
					this.glist = arr
				})
			},
			getbatchs(){
				this.show5 = true
			},
			change3(e){
				this.form.newgoodsbatchs = e.result
			},
			quedingg(item){
				let ids = []
				let newbats = []
				let truenums = []
				for(let item1 of this.dataList2.list) {
					if(item1.positionId == item.positionId){
						for(let item2 of item1.list2) {
							ids.push(item2.id)
							newbats.push(item2.producetime)
							truenums.push(item2.num)
						}
					}
				}
				uni.showModal({
					content: '确定提交吗？',
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/rest/moveDisk/appsubmit?ids='+ids+'&newbats='+newbats+'&truenums='+truenums,{
							},{
								params: {
									goodsId:item.goodsId
								},
								header: {
									'Authorization' :uni.getStorageSync('token'),
								}
							}).then(res => {
								if(res.data.code == 200) {
									this.$refs.uToast.show({
										title: '操作成功',
										type: 'success'
									})
									this.findByGoods(this.xuanzeGoodsId)
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			xuanzegoods(item){
				this.xuanzeGoodsId = item.id
				this.sousuotype = '3'
				this.findByGoods(item.id)
				this.popupShow2 = false
			},
			findByGoods(goodsId){
				this.$http.post('/rest/moveDisk/findByGoods',{
				},{
					params: {
						goodsId:goodsId,
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					if(res.data.data != null && res.data.data != ''){
						this.dataList2 = res.data.data
						this.findtype = 'g'
					} else {
						uni.showToast({
							title: "暂无库存数据",
							icon: 'none'
						})
					}
				})
			},
			search(){
				this.$http.post('/rest/storage/goodslist',{
				},{
					params: {
						condition:this.keyword
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					let arr = []
					let inta = 0
					for(let item1 of res.data.data) {
						if(inta != 10){
							arr.push(item1)
						} else {
							break
						}
						inta++
					}
					this.glist = arr
					this.popupShow2 = true
				})
			},
			search2(){
				this.keyword2 = this.keyword2.toLocaleUpperCase()
				if(typeof this.keyword2 == 'undefined' || this.keyword2 == null || this.keyword2 == ""){
					uni.showToast({
						title: "请输入库位",
						icon: 'none'
					})
					return
				}
				this.sousuotype = '1'
				this.searchPositionNumber = this.keyword2
				this.findByListGoodsType(this.keyword2)
			},
			findByListGoodsType(positionNumber){
				this.$http.post('/rest/moveDisk/findByListGoodsType',{
				},{
					params: {
						positionNumber:positionNumber,
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					if(res.data.data != null && res.data.data != ''){
						this.dataList = res.data.data
						this.findtype = 'p'
					} else {
						uni.showToast({
							title: "暂无库存数据",
							icon: 'none'
						})
						this.dataList = []
						this.findtype = ''
					}
				})
			},
			quedingp(item){
				let ids = []
				let newbats = []
				let truenums = []
				for(let item1 of this.dataList.list) {
					if(item1.goodsId == item.goodsId){
						for(let item2 of item1.list2) {
							ids.push(item2.id)
							newbats.push(item2.goodsbatchs)
							truenums.push(item2.num)
						}
					}
				}
				uni.showModal({
					content: '确定提交吗？',
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/rest/moveDisk/appsubmit?ids='+ids+'&newbats='+newbats+'&truenums='+truenums,{
							},{
								params: {
									goodsId:item.goodsId
								},
								header: {
									'Authorization' :uni.getStorageSync('token'),
								}
							}).then(res => {
								if(res.data.code == 200) {
									this.$refs.uToast.show({
										title: '操作成功',
										type: 'success'
									})
									if(this.sousuotype == '1'){
										this.findByListGoodsType(this.searchPositionNumber)
									} else if(this.sousuotype == '2'){
										this.findByListGoodsType2(this.confirmPositionId)
									}
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			change2(e){
				for(let item1 of this.dataList2.list) {
					if(item1.positionId == this.ginfo.positionId){
						for(let item2 of item1.list2) {
							if(item2.id == this.ginfo.id){
								item2.producetime = e.result
							}
						}
					}
				}
			},
			change(e){
				for(let item1 of this.dataList.list) {
					if(item1.goodsId == this.pinfo.goodsId){
						for(let item2 of item1.list2) {
							if(item2.id == this.pinfo.id){
								item2.producetime = e.result
							}
						}
					}
				}
			},
			getTg(item){
				this.ginfo = item
				this.show4 = true
			},
			getT(item){
				this.pinfo = item
				this.show3 = true
			},
			confirm(e) {
				if(e[2].value == null || e[2].value == ''){
					return
				}
				this.sousuotype = '2'
				this.confirmPositionId = e[2].value
				this.findByListGoodsType2(e[2].value)
				this.popupShow3 = false
			},
			findByListGoodsType2(positionId){
				this.$http.post('/rest/moveDisk/findByListGoodsType',{
				},{
					params: {
						positionId:positionId,
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					if(res.data.data != null && res.data.data != ''){
						this.dataList = res.data.data
						this.findtype = 'p'
					} else {
						uni.showToast({
							title: "暂无库存数据",
							icon: 'none'
						})
					}
				})
			},
			getp(){
				this.popupShow3 = true
			},
		},
	}
</script>

<style lang="scss">
	.u-body-item {
		padding: 20rpx 15rpx;
	}
	.wrap {
		padding: 0rpx 12rpx;
	}

	.u-row {
		margin: 10rpx 2rpx;
	}

	.demo-layout {
		height: 40rpx;
		border-radius: 8rpx;
		line-height: 40rpx;
	}
	.demo-layout2 {
		height: 140rpx;
		border-radius: 8rpx;
	}
	.demo-layout3 {
		height: 900rpx;
		border-radius: 8rpx;
	}

	.bg-purple {
		background: #ffffff;
	}

	.bg-purple-light {
		background: #e5e9f2;
	}

	.bg-purple-dark {
		background: #99a9bf;
	}
	.weikaishi2{
		font-size: 36rpx;
	}
	.weikaishi3{
		font-size: 30rpx;
	}
	.title{
		margin: 30rpx;
	}
	.m-top{
		margin-top: 20rpx;
	}
</style>
